<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>地点关键字 + 公交路线规划</title>
    <style type="text/css">
        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
    <style type="text/css">
        #panel {
            position: fixed;
            background-color: white;
            max-height: 90%;
            overflow-y: auto;
            top: 10px;
            right: 10px;
            width: 280px;
        }

        #panel .amap-call {
            background-color: #009cf9;
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
        }

        #panel .amap-lib-transfer {
            border-bottom-left-radius: 4px;
            border-bottom-right-radius: 4px;
            overflow: hidden;
        }

        .box {
            position: absolute;
            top: 0;
            left: 50px;
            width: 299px;
            padding: 15px;
            background-color: rgba(0, 0, 0, .3);
        }

        .box p {
            display: inline-block;
            margin: 10px 0;
        }

        .box p input {
            display: inline-block;
            width: 215px !important;
        }

        .box button {
            margin-left: 36px;
        }
    </style>
    <script type="text/javascript">
        window._AMapSecurityConfig = {
            securityJsCode: 'fb0cfe46b66d7af00b07e23357dff10d',
        }
    </script>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
    <script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=3861c766a1ec2a5c04b2257861866158&plugin=AMap.Transfer"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>

<body>
    <div id="container"></div>
    <div id="panel"></div>
    <div class="box">
        <p>起点：<input type="text" id="start"></p>
        <p>终点：<input type="text" id="end"></p>
        <button>查询路线</button>
    </div>
    <script type="text/javascript">
        var map = new AMap.Map("container", {
            resizeEnable: true,
            center: [108.885357, 34.226204],
            zoom: 13
        });
        var transOptions = {
            map: map,
            city: '北京市',
            panel: 'panel',
            policy: AMap.TransferPolicy.LEAST_TIME //乘车策略
        };
        //构造公交换乘类
        var transfer = new AMap.Transfer(transOptions);

        // 点击查询按钮显示查询到的公交路线
        let btn = document.querySelector(".box button")
        btn.addEventListener("click", function () {
            let start = document.querySelector("#start").value
            let end = document.querySelector("#end").value
            if (start === '' || end === "") {
                return
            }
            //根据起、终点名称查询公交换乘路线
            transfer.search([{ keyword: start }, { keyword: end, city: '北京' }], function (status, result) {
                if (status === 'complete') {
                    log.success('绘制公交路线完成')
                } else {
                    log.error('公交路线数据查询失败' + result)
                }
            });
        })
    </script>
</body>

</html>